package com.zpark.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.entity.Product;
import com.zpark.service.IProductService;
import com.zpark.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("products")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    @RequestMapping("hot_list")
    public JsonResult<List<Product>> getHotList(){
        List<Product> data = productService.findByHotList();
        return new JsonResult<List<Product>>(OK, data);
    }

    @RequestMapping("{id}/details")
    public JsonResult<Product> getById(@PathVariable Integer id){
        Product data = productService.findById(id);
        return new JsonResult<Product>(OK, data);
    }

    @RequestMapping("all_list")
    public JsonResult<PageInfo<Product>> getAllList(@RequestParam(defaultValue = "0", value = "pageNum") Integer pageNum, @RequestParam(defaultValue = "8", value = "pageSize") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Product> data = productService.findByAllList();
        PageInfo<Product> pageInfo = new PageInfo<Product>(data);
        return new JsonResult<PageInfo<Product>>(OK, pageInfo);
    }

    @RequestMapping("new_list")
    public JsonResult<List<Product>> getNewList(){
        List<Product> data = productService.findNewList();
        return new JsonResult<List<Product>>(OK, data);
    }

    @RequestMapping("idea_list")
    public JsonResult<List<Product>> getIdeaList(){
        List<Product> data = productService.findIdeaList();
        return new JsonResult<List<Product>>(OK, data);
    }

}
